package drops.gui;

import javax.swing.*;

import com.sun.org.apache.xml.internal.utils.ThreadControllerWrapper.ThreadController;

import drops.dao.Patient;

import java.lang.*;
import java.text.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;

public class MainFrame extends JFrame implements ActionListener, MouseListener {
    JDesktopPane jdpDesktop;
    
    JPanel menu;
    JPanel anothermenu;
    
    JToolBar buttons;
    
    JButton searchButton;
    JButton patientButton;
    JButton transactionButton;
    JButton exitButton;
    
    DateLabel dateLabel;
    JLabel status;
    
    String date;
    static int openFrameCount = 0;
    
    ImageIcon searchFrameIcon;
    ImageIcon transactionFrameIcon;
    ImageIcon patientFrameIcon;
    ImageIcon calendarFrameIcon;
    ImageIcon dropsFrameIcon;
    
    SearchPanel searchPanel;
	PatientPanel patientPanel;
	TransactionPanel transactionPanel;
	AddPatientPanel addPatientPanel;
	AddTransactionPanel addTransactionPanel;
	CalendarPanel calendarPanel;
	DentalRecordPanel dentalRecordPanel;
	EditTeethPanel editTeethPanel;
	EditTransactionPanel editTransactionPanel;
	
	JInternalFrame searchFrame;
	JInternalFrame patientFrame;
	JInternalFrame transactionFrame;
	JInternalFrame addPatientFrame;
	JInternalFrame addTransactionFrame;
	JInternalFrame calendarFrame;
	JInternalFrame dentalRecordFrame;
    JInternalFrame editTeethFrame;
    JInternalFrame editTransactionFrame;
    
	public MainFrame() {
        super("DROPS");
        
		searchPanel = new SearchPanel(this);
		patientPanel = new PatientPanel(this);
		transactionPanel = new TransactionPanel(this);
		calendarPanel = new CalendarPanel(this);
		addPatientPanel = new AddPatientPanel(this);
		addTransactionPanel = new AddTransactionPanel(this);
		dentalRecordPanel = new DentalRecordPanel(this);
		editTeethPanel = new EditTeethPanel(this);
		editTransactionPanel = new EditTransactionPanel(this);

		searchFrameIcon = new ImageIcon("images/frameicons/dental.png");
		transactionFrameIcon = new ImageIcon("images/frameicons/transaction.png");
		patientFrameIcon = new ImageIcon("images/frameicons/patient.png");
		calendarFrameIcon = new ImageIcon("images/frameicons/calendar_icon.jpg");
		dropsFrameIcon = new ImageIcon("images/frameicons/drop.png");
		
		date = new Date().toString(); 
		
		status = new JLabel("<html><i>status here...</i></html>");
		
		menu = new JPanel();
		anothermenu = new JPanel();
		buttons = new JToolBar();
		
		searchButton = new JButton("Search");
		searchButton.addActionListener(this);
		searchButton.addMouseListener(this); 
		patientButton = new JButton("Patient");
		patientButton.addActionListener(this);
		patientButton.addMouseListener(this); 
		transactionButton = new JButton("Transaction");	
		transactionButton.addActionListener(this);
		transactionButton.addMouseListener(this); 
		exitButton = new JButton("Exit");
		exitButton.addActionListener(this);
		exitButton.addMouseListener(this); 
		dateLabel = new DateLabel();
		Thread t = new Thread(dateLabel);
		new Thread(dateLabel).start();
		dateLabel.addMouseListener(this); 
		
		menu.setLayout(new FlowLayout());
		menu.add(searchButton);
		menu.add(patientButton);
		menu.add(transactionButton);
		anothermenu.setLayout(new FlowLayout());
		anothermenu.add(dateLabel);
		anothermenu.add(exitButton);
		buttons.setLayout(new BorderLayout());
		buttons.add(menu, BorderLayout.WEST);
		buttons.add(anothermenu, BorderLayout.EAST);
		
        //Make the main window positioned as 50 pixels from each edge of the screen.
        int inset = 50;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(inset, inset, screenSize.width - inset*2, screenSize.height-inset*2);

        //Add a Window Exit Listener
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });


        //Create and Set up the GUI.
        jdpDesktop = new JDesktopPane(); //a specialized layered pane to be used with JInternalFrames
        //createFrame(); //Create first window
        setLayout(new BorderLayout());
        
        jdpDesktop.setBackground(Color.WHITE);

		searchFrame = new JInternalFrame("Search", true, true, true);
		searchFrame.add(searchPanel);
		searchFrame.pack();
		jdpDesktop.add(searchFrame);
		
		patientFrame = new JInternalFrame("Patient", true, true, true);
		patientFrame.add(patientPanel);
		patientFrame.pack();
		jdpDesktop.add(patientFrame);
		
		transactionFrame = new JInternalFrame("Transaction", true, true, true);
		transactionFrame.add(transactionPanel);
		transactionFrame.pack();
		jdpDesktop.add(transactionFrame);

		addPatientFrame = new JInternalFrame("Add Patient", true, true, true);
		addPatientFrame.add(addPatientPanel);
		addPatientFrame.pack();
		jdpDesktop.add(addPatientFrame);

		addTransactionFrame = new JInternalFrame("Add Transaction", true, true, true);
		addTransactionFrame.add(addTransactionPanel);
		addTransactionFrame.pack();
		jdpDesktop.add(addTransactionFrame);
		
		calendarFrame = new JInternalFrame("Calendar", true, true, true);
		calendarFrame.add(calendarPanel);
		calendarFrame.pack();
		jdpDesktop.add(calendarFrame);
		
		dentalRecordFrame = new JInternalFrame("Dental Record", true, true, true);
        dentalRecordFrame.add(dentalRecordPanel);
        dentalRecordFrame.pack();
        jdpDesktop.add(dentalRecordFrame);
		
        editTeethFrame = new JInternalFrame("Edit Tooth", true, true, true, true);
        editTeethFrame.add(editTeethPanel);
        editTeethFrame.pack();
        jdpDesktop.add(editTeethFrame);
        
        editTransactionFrame = new JInternalFrame("Edit Transaction", true, true, true, true);
        editTransactionFrame.add(editTransactionPanel);
        editTransactionFrame.pack();
        jdpDesktop.add(editTransactionFrame);
		
		add(buttons,BorderLayout.NORTH);
        add(jdpDesktop,BorderLayout.CENTER);
        add(status,BorderLayout.SOUTH);

        //Make dragging faster by setting drag mode to Outline
        jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
        
        loadBackgroundImage();
    }
    
    public void searchFrame() {
    	if (searchFrame.getParent()!=jdpDesktop)
    		jdpDesktop.add(searchFrame);
    	searchFrame.setSize(400,200);
    	searchFrame.setVisible(true);
    	searchFrame.moveToFront();
    	searchFrame.setFrameIcon(searchFrameIcon);
    }
    
    public void patientFrame() {
    	if (patientFrame.getParent()!=jdpDesktop)
    		jdpDesktop.add(patientFrame);
    	patientFrame.setVisible(true);
    	patientFrame.moveToFront();
    	patientFrame.setFrameIcon(patientFrameIcon);
    }

    public void transactionFrame() {
    	if (transactionFrame.getParent()!=jdpDesktop)
    		jdpDesktop.add(transactionFrame);
    	transactionFrame.setVisible(true);
    	transactionFrame.moveToFront();
    	transactionFrame.setFrameIcon(transactionFrameIcon);
    }
    
    public void addPatientFrame() {
    	if (addPatientFrame.getParent()!=jdpDesktop)
    		jdpDesktop.add(addPatientFrame);
    	addPatientFrame.setSize(300, 300);
    	addPatientFrame.setVisible(true);
    	addPatientFrame.moveToFront();
    }
    
    public void closeAddPatientFrame() {
    	addPatientFrame.dispose();
    }
    
    
    public void addTransactionFrame() {
    	if (addTransactionFrame.getParent()!=jdpDesktop)
    		jdpDesktop.add(addTransactionFrame);
    	addTransactionFrame.setSize(300, 300);
    	addTransactionFrame.setVisible(true);
    	addTransactionFrame.moveToFront();
    }

    public void closeAddTransactionFrame() {
    	addTransactionFrame.dispose();
    }

    public void calendarFrame() {
    	if (calendarFrame.getParent()!=jdpDesktop)
    		jdpDesktop.add(calendarFrame);
    	calendarFrame.setVisible(true);
    	calendarFrame.moveToFront();
    	calendarFrame.setResizable(false);
		calendarFrame.setFrameIcon(calendarFrameIcon);
    }
    
    public void dentalRecordFrame() {
    	if (dentalRecordFrame.getParent()!=jdpDesktop)
    		jdpDesktop.add(dentalRecordFrame);
    	dentalRecordFrame.setVisible(true);
    	dentalRecordFrame.moveToFront();
    }
    
   
    public void editTeethFrame() {
    	if (editTeethFrame.getParent()!=jdpDesktop)
    		jdpDesktop.add(editTeethFrame);
    	editTeethFrame.setVisible(true);
    	editTeethFrame.moveToFront();
    }
    
    public void closeEditTeethFrame() {
    	editTeethFrame.dispose();
    }
    
    public void setTooth(int toothNumber){
    	editTeethPanel.setTooth(toothNumber);
    }
    
    public void editTransactionFrame() {
    	if (editTransactionFrame.getParent()!=jdpDesktop)
    		jdpDesktop.add(editTransactionFrame);
    	editTransactionFrame.setVisible(true);
    	editTransactionFrame.moveToFront();
    }
    
    
    public void closeEditTransactionFrame() {
    	editTransactionFrame.dispose();
    }
    
    public void searchTransaction(String input, String category) {
    	transactionPanel.search(input, category);
    }
    
    public void searchTransaction(Date input, String category) {
    	transactionPanel.search(input,category);
    }
    
    
    public void refresh() {
    	patientPanel.refresh();
    	dentalRecordPanel.refresh();
    	transactionPanel.refresh();
    }
    public void refreshPatients() {
    	if (patientFrame.getParent()==jdpDesktop&&patientFrame.isVisible()) {
    		patientPanel.refresh();
    	}
    }
    public void refreshTransactions() {
    	dentalRecordPanel.refresh();
    	transactionPanel.refresh();
    }
    
    public void setPatient(int patientNumber) {
    	dentalRecordPanel.setPatient(patientNumber);
    	addTransactionPanel.setPatientNumber(patientNumber);
    	editTeethPanel.setPatient(patientNumber);
    }
    
    public void setTransaction(int transactionNumber) {
    	editTransactionPanel.setTransactionNumber(transactionNumber);
    }
    
    public void searchPatient(String input, String category) {
    	patientPanel.search(input,category);
    }
    
    class MyInternalFrame extends JInternalFrame {
        
        static final int xPosition = 30, yPosition = 30;

        public MyInternalFrame() {
            super("IFrame #" + (++openFrameCount), 
                  true, //resizable
                  true, //closable
                  true, //maximizable
                  true);//iconifiable

            setSize(300,300);

            //Set the window's location.
            setLocation(xPosition*openFrameCount, yPosition*openFrameCount);
        }
    }
    
    public static void main(String[] args) {
        InitTorque it = new InitTorque();
        it.init();
        
        try {
    	    // Set System L&F
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        } 
        catch (UnsupportedLookAndFeelException e) {
           // handle exception
        }
        catch (ClassNotFoundException e) {
           // handle exception
        }
        catch (InstantiationException e) {
           // handle exception
        }
        catch (IllegalAccessException e) {
           // handle exception
        }

        
        MainFrame frame = new MainFrame();
        frame.setVisible(true);
        frame.setTitle("Dental Records of Patients Software - DROPS");
    }
    
    public void actionPerformed(ActionEvent e) {
    	if(e.getSource()==searchButton)
    		searchFrame();
        if(e.getSource()==patientButton){
            patientFrame();
            searchPatient("", "Name");
        }
        if(e.getSource()==transactionButton){
            transactionFrame();
            searchTransaction("", "Description");
        }
        if(e.getSource()==exitButton)
        	System.exit(0);
    }
    

	public void mouseClicked(MouseEvent arg0) {
		if(arg0.getSource()==dateLabel)
			calendarFrame();
	}

	
	public void mouseEntered(MouseEvent arg0) {
		if(arg0.getSource()==searchButton){
			status.setText("<html><i>search for a patient or a transaction...</i></html>");
		}	
		if(arg0.getSource()==transactionButton){
			status.setText("<html><i>add a transaction...</i></html>");
		}		
		if(arg0.getSource()==patientButton){
			status.setText("<html><i>add a patient...</i></html>");
		}		
		if(arg0.getSource()==exitButton){
			status.setText("<html><i>exit DROPS...</i></html>");
		}
		if(arg0.getSource()==dateLabel){
			status.setText("<html><i>click to show calendar...</i></html>");
		}	
	}

	public void mouseExited(MouseEvent arg0) {
		if(arg0.getSource()==searchButton){
			status.setText("<html><i>status here...</i></html>");
		}
		if(arg0.getSource()==transactionButton){
			status.setText("<html><i>status here...</i></html>");
		}	
		if(arg0.getSource()==patientButton){
			status.setText("<html><i>status here...</i></html>");
		}	
		if(arg0.getSource()==exitButton){
			status.setText("<html><i>status here...</i></html>");
		}
		if(arg0.getSource()==dateLabel){
			status.setText("<html><i>status here...</i></html>");
		}	
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub	
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void loadBackgroundImage() {
			ImageIcon bg = new ImageIcon("images/bg.png");
		    JLabel bgLabel = new JLabel(bg);
		    int inset = 50;
		    
		    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			bgLabel.setBounds(0, 0, screenSize.width - inset*(-4), screenSize.height-inset*4);
			jdpDesktop.add(bgLabel, new Integer(Integer.MAX_VALUE));
	
		}
}
    
